package com.ruoyi.edh.service.impl;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jmessage.api.JMessageClient;
import cn.jmessage.api.common.model.RegisterInfo;
import cn.jmessage.api.common.model.UserPayload;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.IMUserModel;
import com.ruoyi.edh.service.IIMService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @version :
 * @program: easyBuy
 * @description:
 * @author: 周爱鸿
 * @create: 2020-11-06 14:44
 **/
@Component
@Slf4j
public class IIMServiceImpl implements IIMService {

	@Autowired
	private JMessageClient jMessageClient;

	@Override
	public Integer register(SysUser user) {
		RegisterInfo registerInfos
					= RegisterInfo.newBuilder()
					.setUsername(user.getUserName())
					.setPassword("123456")
					.setNickname(user.getNickName())
					.setAvatar(user.getAvatar())
					.setGender(1)
					.setAddress(user.getAddress())
					.build();
		try {
			String s = jMessageClient.registerUsers(new RegisterInfo[]{registerInfos});
			log.info("极光"+user.getUserName()+"用户注册 :" + s);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			return 0;
		} catch (APIRequestException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	@Override
	public Integer update(SysUser user) {
		UserPayload userPayload = UserPayload.newBuilder()
				.setNickname(user.getNickName())
				.setAvatar(user.getAvatar())
				.setAddress(user.getAddress())
				.setGender(Integer.valueOf(user.getSex()))
				.build();
		try {
			jMessageClient.updateUserInfo(user.getUserName(),userPayload);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			return 0;
		} catch (APIRequestException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}
}
